<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/18
 * Time: 11:51
 */
namespace api\controllers;

use common\services\GeographyService;
use Yii;

class GeographyController extends BaseController
{
    /**
     * 根据请求接口的ip地址获取用户当前城市的天气
     * 若传递城市编号时，返回对应编号的城市天气
     * adcode 编号
     * @return array
     */
    public function actionWeather()
    {
        $adcode = trim($this->request->post('adcode', ''));

        $geography = new GeographyService();
        // 获取当前城市的code
        if (empty($adcode)) {
            $adcode = $geography->adcode();
        }
        // 先查询数据库是否已经获取过该小时点的天气信息
        $result = $geography->findOneWeather($adcode);
        if (empty($result)) {
            // 请求获取天气信息
            $weather = $geography->weather($adcode);
            $result = $geography->saveWeather($weather);
        }
        return $this->endReponse('0',$result);
    }

    /**
     * 搜索城市名称，获取对应编号
     * city_name 城市名称
     * @return array
     */
    public function actionCityCode()
    {
        $city = trim($this->request->post('city_name', ''));
        $geography = new GeographyService();
        $result = $geography->cityCode($city);
        return $this->endReponse('0',$result);
    }


}